<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2019/12/19
 * Time: 14:20
 */
namespace app\common\lib;
use think\facade\Db;
use util\CommonUtil;

class AdminAuth
{
    protected static $instance = null;
    protected $admin = null;
    protected $admin_auth;
    protected $menu_list = [];
    protected $root_id = 1;
    private function __construct()
    {
        $this->initSession();
        $this->menu_list = $this->getRuleList();
    }

    public static function instance(){
        if(empty(self::$instance)){
            self::$instance = new static();
        }
        return self::$instance;
    }
    private function initSession(){
        $this->admin_auth = session('admin_auth');
        $this->admin = session('admin_info');
    }
    public function isLogin(){
        if(empty($this->admin_auth)){
            $this->initSession();
        }
        $admin = $this->admin_auth;
        if (empty($admin)) {
            return false;
        } else {
            return session('admin_auth_sign') == CommonUtil::instance()->data_auth_sign($admin) ? $admin['id'] : false;
        }
    }

    public function isRoot(){
        return $this->admin_auth['id'] == $this->root_id;
    }

    public function getAdmin(){
        if(empty($this->admin)){
            $this->initSession();
        }
        return $this->admin;
    }
    public function getMenuList(){
        return $this->menu_list;
    }
    public function getRootId(){
        return $this->root_id;
    }
    public function match($arr){
        $action = parse_name(request()->action());
        $arr = array_map('parse_name',$arr);
        return in_array($action,$arr);
    }
    public function authCheck($url = ''){
        if($this->isRoot()){
            return true;
        }
        $url_list = $this->getRuleUrl();
        return in_array($url,$url_list);
    }

    public function getGroup($admin_id = null){
        $admin_id = $admin_id ?: $this->admin_auth['id'];
        $group_ids = Db::name('auth_group_access')
            ->where('admin_id',$admin_id)
            ->where('delete_time',DATA_DISABLE)
            ->column('group_id');
        return Db::name('auth_group')->whereIn('id',$group_ids)->select();
    }

    public function getRuleList(){
        if(!$this->isRoot()){
            $group_list = $this->getGroup();
            $menu_ids = [];
            foreach ($group_list as $group){
                // 合并多个分组的权限节点并去重
                if(!empty($group['rules'])){
                    $menu_ids = array_merge($menu_ids, explode(',', trim($group['rules'], ',')));
                }
            }
            $menu_ids = array_unique($menu_ids);
            if(empty($menu_ids)){
                return $menu_ids;
            }
            $this->menu_list = Db::name('admin_menu')
                ->whereIn('id',$menu_ids)
                ->where('delete_time',DATA_DISABLE)
                ->order('sort','ASC')
                ->select();
            return $this->menu_list;
        }else{
            $this->menu_list = Db::name('admin_menu')
                ->where('delete_time',DATA_DISABLE)
                ->order('sort','ASC')
                ->select();
            return $this->menu_list;
        }
    }

    public function getRuleUrl(){
        $auth_menu_list = $this->menu_list;
        $auth_list = [];
        foreach ($auth_menu_list as $info) {
            $auth_list[] = $info['url'];
        }
        return $auth_list;
    }

    public function __get($name)
    {
        if(empty($this->admin)){
            $this->initSession();
        }
        return isset($this->admin[$name]) ? $this->admin[$name] : null;
    }
    private function __clone()
    {
    }
}